//
//  MTRequestedViewController.m
//  ShiShiBang
//
//  Created by loujianwei on 16/9/24.
//  Copyright © 2016年 motianjituan. All rights reserved.
//

#import "MTRequestedViewController.h"
#import "TopView.h"
#import <Masonry.h>
#import "MTAlreadBargainViewController.h"
#import "MTWaitInvitationViewController.h"
#import "MTAlreadInvitationViewController.h"
#import "MTAlreadOverdueViewController.h"

static NSString * const ID = @"cell";
@interface MTRequestedViewController () <UICollectionViewDataSource,UICollectionViewDelegate>

/**  */
@property (nonatomic, weak)  UIView *titleView;
@property (nonatomic, weak) UIButton *selectButton;
@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *buttons;
@property (nonatomic, weak) UIView *underLineView;

@end

@implementation MTRequestedViewController
#pragma mark - 懒加载按钮数组
- (NSMutableArray *)buttons
{
    if (_buttons == nil) {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = MTColor(239, 239, 244);
    self.navigationItem.title = @"有求必应";

    // 添加底部内容view
    [self setupBottomView];

    //添加顶部View
    [self addTopView];

    //添加子控制器
    [self setUpChildViewController];
    
    //设置标题View
    [self setUpTitleView];

    // 不需要自动添加额外滚动区域
    self.automaticallyAdjustsScrollViewInsets = NO;
}

#pragma mark -添加标题View
- (void)setUpTitleView {

    CGFloat titleViewX = 0;
    CGFloat titleViewY =76.5;
    CGFloat titleViewW = MTScreenW;
    CGFloat titleViewH = 43;
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(titleViewX, titleViewY, titleViewW, titleViewH)];
    self.titleView = titleView;
    titleView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:titleView];

    NSInteger count = self.childViewControllers.count;
    CGFloat titleBtnW = MTScreenW / count;
    CGFloat titleBtnH = titleView.mt_height;
    CGFloat titleBtnX = 0;
    CGFloat titleBtnY = 0;
    for (int i = 0; i < count; i++) {

    UIButton *titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    titleBtn.tag = i;
    UIViewController *vc = self.childViewControllers[i];
    [titleBtn setTitle:vc.title forState:UIControlStateNormal];
    [titleBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [titleBtn setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
    titleBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    titleBtnX = titleBtnW * i;
    titleBtn.frame = CGRectMake(titleBtnX, titleBtnY, titleBtnW, titleBtnH);
    [titleView addSubview:titleBtn];
    [self.buttons addObject:titleBtn];
        // 监听按钮
         [titleBtn addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {

            // 添加下划线
            UIView *underLineView = [[UIView alloc] init];
            underLineView.backgroundColor = [UIColor greenColor];
            [titleView addSubview:underLineView];
            _underLineView = underLineView;

            // 中心点x
            underLineView.mt_height = 2;
            underLineView.mt_y = titleView.mt_height - underLineView.mt_height;
            // 1.根据控件内容计算尺寸,然后给控件设置尺寸
            [titleBtn.titleLabel sizeToFit];
            underLineView.mt_width = titleBtn.titleLabel.mt_width + 50;
            // 一定要先设置尺寸 在设置中心点
            underLineView.mt_centerx = titleBtn.mt_centerx;


            [self titleClick:titleBtn];
        }
    }
}

#pragma mark - 点击标题
- (void)titleClick:(UIButton *)button
{
    NSInteger i = button.tag;
    // 1.选中按钮
    [self selButton:button];

    // 2.设置collectionView偏离量
    CGFloat offsetX = i * MTScreenW;
    _collectionView.contentOffset = CGPointMake(offsetX, 0);
}

#pragma mark - UICollectionViewDelegate
// 滚动完成的时候就会调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 获取页码
    NSInteger page = scrollView.contentOffset.x / MTScreenW;

    // 获取标题按钮
    UIButton *titleButton = self.buttons[page];

    // 选中标题
    [self selButton:titleButton];
}

#pragma mark - 选中按钮
- (void)selButton:(UIButton *)button
{
    _selectButton.selected = NO;
    button.selected = YES;
    _selectButton = button;

    // 修改下划线位置
    [UIView animateWithDuration:0.25 animations:^{
        _underLineView.mt_centerx = button.mt_centerx;
        
    }];
}
#pragma mark -添加所有子控制器
- (void)setUpChildViewController {

    //待应邀
    MTWaitInvitationViewController *waitInVC = [[MTWaitInvitationViewController alloc] init];
    waitInVC.title = @"待应邀";
    [self addChildViewController:waitInVC];

    //已应邀
    MTAlreadInvitationViewController *alreadInVC = [[MTAlreadInvitationViewController alloc] init];
    alreadInVC.title = @"已应邀";
    [self addChildViewController:alreadInVC];

    //已成交
    MTAlreadBargainViewController *alreadBargainVC = [[MTAlreadBargainViewController alloc] init];
    alreadBargainVC.title = @"已成交";
    [self addChildViewController:alreadBargainVC];

    //已过期
    MTAlreadOverdueViewController *alreadOverdueVC = [[MTAlreadOverdueViewController alloc] init];
    alreadOverdueVC.title = @"已过期";
    [self addChildViewController:alreadOverdueVC];

}

#pragma mark -添加顶部View
- (void)addTopView {

    UIView *topBackView = [[UIView alloc] init];
    topBackView.backgroundColor = MTColor(239, 239, 244);
    [self.view addSubview:topBackView];

    TopView *leftView = [[TopView alloc] init];
    leftView.backgroundColor = [UIColor whiteColor];
    [leftView setUpTopChildView:YES];
    [topBackView addSubview:leftView];

    [topBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.equalTo(@(62.5));
    }];
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topBackView.mas_left);
        make.top.equalTo(topBackView.mas_top);
        make.width.equalTo(@((MTScreenW / 2) - 1));
        make.height.equalTo(topBackView.mas_height);
    }];

    TopView *rightView = [[TopView alloc] init];
    rightView.backgroundColor = [UIColor whiteColor];
    [rightView setUpTopChildView:NO];
    [topBackView addSubview:rightView];

    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(topBackView.mas_right);
        make.top.equalTo(topBackView.mas_top);
        make.width.equalTo(@((MTScreenW / 2) - 1));
        make.height.equalTo(topBackView.mas_height);
    }];

}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.childViewControllers.count;
}

// 只要有新的cell出现就会调用
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];

    // 移除其它子控制器view
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    // 添加对应的子控制器view 到 对应cell
    UIViewController *vc = self.childViewControllers[indexPath.row];
    // 添加到contentView
    [cell.contentView addSubview:vc.view];

//    cell.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];
//
    return cell;
}


#pragma mark - 添加底部内容view
- (void)setupBottomView
{
    // 布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//    layout.itemSize = [UIScreen mainScreen].bounds.size;
    layout.itemSize = CGSizeMake(MTScreenW, MTScreenH - 121.5);

    // 设置滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;

    // UICollectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100.5, MTScreenW, MTScreenH - 121.5) collectionViewLayout:layout];
    collectionView.backgroundColor = MTColor(215, 215, 215);
    [self.view addSubview:collectionView];
    // 不显示水平指示器
    collectionView.showsHorizontalScrollIndicator = NO;
    // 开启分页
    collectionView.pagingEnabled = YES;
    _collectionView = collectionView;
    // 设置collectionView数据源,展示cell,需要把子控制器view添加cell
    collectionView.dataSource = self;
    collectionView.delegate = self;

    // 注册cell
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
}

@end
